package com.houseman.workFlow.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.houseman.workFlow.bean.WorkFlowBean;
import com.houseman.workFlow.dao.WorkFlowDao;
import com.houseman.workFlow.utils.DBUtils;

public class WorkFlowDaoImpl implements WorkFlowDao{

	/**
	 * 获取流程信息
	 * @param workId
	 * @return
	 * @throws Exception
	 */
	public WorkFlowBean getWorkFlowInfo(int workId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT WORKID,GRAPHID,NAME,PROCLASS,APPROVER,STEP,ISROLLBACK,STATUS,TRUSTEESHIP,CREATER,CREATEDATE,UPDATER,UPDATEDATE FROM WORKFLOW WHERE WORKID=?");
		ps.setInt(1, workId);
		
		ResultSet rs = ps.executeQuery();
		WorkFlowBean bean = new WorkFlowBean();
		while (rs.next()) {
			bean.setWorkId(rs.getInt("WORKID"));
			bean.setGraphId(rs.getInt("GRAPHID"));
			bean.setName(rs.getString("NAME"));
			bean.setProClass(rs.getString("PROCLASS"));
			bean.setApprover(rs.getString("APPROVER"));
			bean.setStep(rs.getInt("STEP"));
			bean.setRollback(rs.getInt("ISROLLBACK"));
			bean.setStatus(rs.getInt("STATUS"));
			bean.setTrusteeship(rs.getInt("TRUSTEESHIP"));
			bean.setCreater(rs.getString("CREATER"));
			bean.setCreateDate(rs.getString("CREATEDATE"));
			bean.setUpdater(rs.getString("UPDATER"));
			bean.setUpdateDate(rs.getString("UPDATEDATE"));
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return bean;
	}

	/**
	 * 启动流程，保存流程信息
	 * @param info
	 * @throws Exception
	 */
	public int startWorkFlow(WorkFlowBean info) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("INSERT INTO WORKFLOW(GRAPHID,NAME,PROCLASS,APPROVER,STEP,STATUS,CREATER,CREATEDATE) VALUES(?,?,?,?,?,?,?,NOW())", Statement.RETURN_GENERATED_KEYS);
		ps.setInt(1, info.getGraphId());
		ps.setString(2, info.getName());
		ps.setString(3, info.getProClass());
		ps.setString(4, info.getApprover());
		ps.setInt(5, info.getStep());
		ps.setInt(6, info.getStatus());
		ps.setString(7, info.getCreater());
		
		ps.execute();
		
		ResultSet rs = ps.getGeneratedKeys();
		int workId = 0;
		while (rs.next()) {
			workId = rs.getInt(1);
		}
		//关闭连接
		DBUtils.close(conn, ps);
		
		return workId;
	}

	/**
	 * 修改流程一下已办理信息
	 * @param workId
	 * @param step
	 * @param approve
	 * @throws Exception
	 */
	public void updateNextStepInfo(WorkFlowBean info) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("UPDATE WORKFLOW SET STEP=?,APPROVER=?,ISROLLBACK=?,UPDATER=?,UPDATEDATE=NOW() WHERE WORKID=?");
		ps.setInt(1, info.getStep());
		ps.setString(2, info.getApprover());
		ps.setInt(3, info.getRollback());
		ps.setString(4, info.getUpdater());
		ps.setInt(5, info.getWorkId());
		
		ps.executeUpdate();
		
		DBUtils.close(conn, ps);
	}

	/**
	 * 关闭流程
	 * @param GRAPHID
	 * @throws Exception
	 */
	public void closeWorkFlow(int workId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("UPDATE WORKFLOW SET STATUS=1 WHERE WORKID=?");
		ps.setInt(1, workId);
		
		ps.executeUpdate();
		
		DBUtils.close(conn, ps);
	}

}
